package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.adapter.AnchorRecyclerAdapter;
import com.lgqz.himalayafm.bean.AnchorRecyclerEntity;
import com.lgqz.himalayafm.http.HttpService;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class AnchorFragment extends Fragment implements Callback<AnchorRecyclerEntity>, PullLoadMoreRecyclerView.PullLoadMoreListener {


    private PullLoadMoreRecyclerView recyclerView;
    private AnchorRecyclerAdapter adapter;
    private int page = 1;
    private boolean isRefresh = true;

    public AnchorFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_anchor, container, false);


        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.anchor_recycler_view);
        recyclerView.setLinearLayout();
        recyclerView.setRefreshing(isRefresh);
        recyclerView.setFooterViewText("数据加载中。。。");
        recyclerView.setOnPullLoadMoreListener(this);
        adapter = new AnchorRecyclerAdapter(getContext());
        recyclerView.setAdapter(adapter);
        initData(page);


    }

    private void initData(int page) {
        HttpService service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(HttpService.class);
        service.getAnchorData("android", page).enqueue(this);
        Log.d("1620", "initData: " + page);
    }

    @Override
    public void onResponse(Call<AnchorRecyclerEntity> call, Response<AnchorRecyclerEntity> response) {
        AnchorRecyclerEntity anchorRecyclerEntity = response.body();
        Log.d("1620", "onResponse:ana " + anchorRecyclerEntity.getList().size());
            adapter.addAll(anchorRecyclerEntity.getList());
            recyclerView.setPullLoadMoreCompleted();
        if (anchorRecyclerEntity.getList().size()==0){
            isRefresh=false;
        }
    }

    @Override
    public void onFailure(Call<AnchorRecyclerEntity> call, Throwable t) {
        Log.d("1620", "onFailure: ");
    }

    @Override
    public void onRefresh() {
        adapter.clearData();
        page = 1;
        initData(page);

    }

    @Override
    public void onLoadMore() {
        page = page + 1;
        initData(page);

    }
}
